

*******************************************************
*******************************************************
***** Cross State Results for the Online Appendix *****
*******************************************************
*******************************************************

cap mkdir `"$output/online_appendix"'
cap mkdir `"$output/log/online_appendix"'

cap log close 
log using `"$output/log/online_appendix/online_appendix_cross_state.smcl"', replace 

di "This run uses code version from 2023/03/29"


//use average commercial fees 
use `"$output/indices/fee_indices.dta"', clear 
keep if pay_type_pooled == 3 
keep payee_state_cd mean_ln_line_item_value
rename mean_ln_line_item_value avg_commercial_fee
tempfile comm_fee
save `comm_fee'

use `"$x_state_data/$x_state_file_regression"', clear 


gen accept_medicare = 1 if medicare == "Y"
replace accept_medicare = 0 if medicare == "N"
replace accept_medicare = . if acceptinsurance == . 
bys npimedicare_id: gegen mean_medicare = mean(accept_medicare)
gen capacity = 1 - mean_medicare


*merge in the indices 
merge m:1 payee_state_cd using `"$indices/fee_indices_medicaid.dta"', keepusing(pi*)
keep if _merge == 3
drop _merge 

merge m:1 payee_state_cd using `"$indices/CIP_indices_medicaid.dta"', keepusing(*CIP*)
keep if _merge == 3
drop _merge 

//merge in the commercial fees 
merge m:1 payee_state_cd using `comm_fee'
keep if _merge == 3
drop _merge 


//merge controls 
merge m:1 year FIPS using `"$control_var/county_controls.dta"'
keep if _merge == 3 // all data from master file merge 
drop _merge 

destring statefips, replace  
merge m:1 year statefips using `"$control_var/control_vars_state.dta"'
keep if _merge == 3 //all data from the master file merge 
drop _merge 



//Use only TIN info to connect groups
gen skaGroup3a=string(ID)
replace skaGroup3a="MDPPASTIN"+tin  if ~missing(tin)
 
//drop missing
drop if missing(paystateid)

cap drop group?
gegen group3 = group(skaGroup3a)
gegen group3y = group(skaGroup3a year)


cap drop totstates?
//cross state variable 
cap drop totstates?
	
sort year group3 paystateid
by year group3 paystateid: gen markstate=_n==1
by year group3: egen totstates3=sum(markstate)
drop markstate


gen ltotdocsALL1=lnumdocs

gen kaiser=CODE3=="KAI"
drop if kaiser


****locals for Oster test
local years "2009.year 2010.year 2011.year 2012.year 2013.year 2014.year 2015.year"

cap drop MDsPerCapita
gen MDsPerCapita = tot_active_md/county_pop

foreach c of varlist veteran unempl poverty white {
	
	replace `c' = `c'/county_pop
}


unab controllist: mean_ma_pen mean_number_mediciad_elig mean_dual mean_unins65 mean_number_medicare_elig mean_medicare_enrol 
unab year_controllist: hinsva hinscare hinscaid hcovany veteran unempl med_hh_ince avg_hhc county_pop tot_active_md poverty white


**enusre controls are notm issingsortsort npimedicare_id YearPost
foreach var of varlist `controllist' `year_controllist' {
	
	keep if `var' != . 
}

unab health_ins_market_year: hinscaid hcovany hinscare avg_hhc mean_ma_pen mean_number_mediciad_elig mean_dual mean_unins65 mean_number_medicare_elig mean_medicare_enrol 
unab health_prov_year: tot_active_md MDsPerCapita
unab ses_year: unempl white county_pop veteran poverty med_hh_ince
	

foreach group of var group3 { 
	
	else if "`group'" == "group3" {
		local cross_state_var totstates3
		local definition "MDPPAS_Tax_ID"
		local group_table "Tax ID"

	}
	

	xtset `group'


	cap drop physind 
	cap drop phys_count
	cap drop group_ind
	gen group_ind = 1 if `cross_state_var'>1 & `group' != . & capacity == 0 
	gen physind = 0
	bys npimedicare_id group_ind: replace physind = 1 if _n == 1 
	replace physind = 0 if group_ind != 1
	gegen phys_count = sum(physind)
	local physcount : di %7.0fc phys_count
	di `physcount'

	cap drop physind 
	cap drop phys_count
	cap drop group_ind
	gen group_ind = 1 if `cross_state_var'>1 & `group' != . 
	gen physind = 0
	bys npimedicare_id group_ind: replace physind = 1 if _n == 1 
	replace physind = 0 if group_ind != 1
	gegen phys_count = sum(physind)
	local physcount_full : di %7.0fc phys_count
	di `physcount_full'
		
		
		
	*******************************************************
	******* Appendix Table S.12 -- Unestricted Sample *****
	*******************************************************
					
	local loss_denial v_initial_den_CIP
	local loss lev_CIP_FE
	local loss_heck lev_CIP_FE_nofe
	local loss_dollar CIP_abs 
	local loss_dollar_heck CIP_abs_nofe
	local fee pi_c_FE_cpt_line
	local fee_level pi_c_FE_cpt_line_abs

	local lossw lev_CIP_FE_W
	local lossw_heck lev_CIP_FE_W_nofe
	local feew pi_c_W_FE_cpt_line


	local loss_name `loss' CIP `loss_heck' CIP 
	local fee_name `fee' Fee

	local lossw_name `lossw' CIP `lossw_heck' CIP 
	local feew_name `feew' Fee
	
	local loss_level_name `loss_dollar' CIP `loss_dollar_heck' CIP 
	local fee_level_name `fee_level' Fee



	//Column 2
	//No selection Correction, Unrestricted sample IV
	ivreghdfe acceptinsurance `fee' (`loss'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if `cross_state_var'>1, absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount_full'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "No"
	eststo mainxstate2		

	//Column 1
	// No selection Correction, Unrestricted sample OLS
	areg acceptinsurance `fee' `loss' `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount_full'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "No"
	eststo mainxstate1

			
	//Column 3
	// Selection Correction, Unrestricted sample OLS
	areg acceptinsurance `fee' `loss_heck' `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount_full'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "No"
	eststo mainxstate3


	//Column 4
	// Selection Correction, Unrestricted sample IV
	ivreghdfe acceptinsurance `fee' (`loss_heck'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount_full'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "No"
	eststo mainxstate4	


	// set output dir to avoid conflict on BEA server 
	cd `"$output/online_appendix"'
	#delimit ;

		//cross state table
		esttab mainxstate1 mainxstate2 mainxstate3 mainxstate4 using "online_appendix_`group'_xstate_unrestricted.tex" ,  replace substitute(_ \_)
		numbers  nomtitles booktabs mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_name' `fee_name')
		keep(Fee CIP ) 
		order(CIP Fee)
		coeflabels(Fee "$\log \pi$ index"
			   CIP "$\tau$ index"
	)
		se b(4) se(4 ) stats(instrument Sample pys_number N group_FE controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Group FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;

					
	#delimit cr
			
			
			
			
			
			
			
			
	**********************************************************
	***** Appendix Table S.17 -- Including Commercial Fees ***
	**********************************************************
					
	//Column 2
	//No selection Correction, Restricted sample IV
	ivreghdfe acceptinsurance `fee' (`loss'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' avg_commercial_fee i.year if `cross_state_var'>1 & capacity == 0, absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	estadd local comm_contr "Yes"
	eststo mainxstate2		

	//Column 1
	// No selection Correction, Restricted sample OLS
	areg acceptinsurance `fee' `loss' `health_ins_market_year' `health_prov_year' `ses_year' avg_commercial_fee i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	estadd local comm_contr "Yes"
	eststo mainxstate1

			
	//Column 3
	// Selection Correction, Restricted sample OLS
	areg acceptinsurance `fee' `loss_heck' `health_ins_market_year' `health_prov_year' `ses_year' avg_commercial_fee i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	estadd local comm_contr "Yes"
	eststo mainxstate3


	//Column 4
	// Selection Correction, Restricted sample IV
	ivreghdfe acceptinsurance `fee' (`loss_heck'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' avg_commercial_fee i.year if e(sample), absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	estadd local comm_contr "Yes"
	eststo mainxstate4	


	// set output dir to avoid conflict on BEA server 
	cd `"$output/online_appendix"'
	#delimit ;

		//cross state table
		esttab mainxstate1 mainxstate2 mainxstate3 mainxstate4 using "online_appendix_`group'_xstate_commfee_contrl.tex" ,  replace substitute(_ \_)
		numbers  nomtitles booktabs mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_name' `fee_name')
		keep(Fee CIP avg_commercial_fee) 
		order(CIP Fee avg_commercial_fee)
		coeflabels(Fee "$\log \pi$ index"
			   CIP "$\tau$ index"
			   avg_commercial_fee "Mean log commercial fee"
	)
		se b(4) se(4 ) stats(instrument Sample pys_number N group_FE controls comm_contr phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Group FE}" "\footnotesize{Controls}" "\footnotesize{Controlling for Commercial Fees}" "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;

					
	#delimit cr



	**********************************************************
	**** Appendix Table S.10 -- Weighted CIP and Fee Index ***
	**********************************************************
					
	//Column 2
	//No selection Correction, Restricted sample IV
	ivreghdfe acceptinsurance `feew' (`lossw'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if `cross_state_var'>1 & capacity == 0, absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate2		

	//Column 1
	// No selection Correction, Restricted sample OLS
	areg acceptinsurance `feew' `lossw' `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate1

			
	//Column 3
	// Selection Correction, Restricted sample OLS
	areg acceptinsurance `feew' `lossw_heck' `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate3


	//Column 4
	// Selection Correction, Restricted sample IV
	ivreghdfe acceptinsurance `feew' (`lossw_heck'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate4	


	// set output dir to avoid conflict on BEA server 
	cd `"$output/online_appendix"'
	#delimit ;

		//cross state table
		esttab mainxstate1 mainxstate2 mainxstate3 mainxstate4 using "online_appendix_`group'_xstate_weighted.tex" ,  replace substitute(_ \_)
		numbers  nomtitles booktabs mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`lossw_name' `feew_name')
		keep(Fee CIP ) 
		order(CIP Fee)
		coeflabels(Fee "$\log \pi$ index"
			   CIP "$\tau$ index"
	)
		se b(4) se(4 ) stats(instrument Sample pys_number N group_FE controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Group FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;

					
	#delimit cr


	
	
	*************************************************************************
	**** Cross-State Specifications with adjusted CIP indices (Table S.8) ***
	*************************************************************************
	local adjusted_cip_indices `"_rf"'

	foreach adj_loss of local adjusted_cip_indices {
		
	local lossadj_name `""lev_CIP_FE_nofe`adj_loss'" " CIP " "lev_CIP_FE`adj_loss'" " CIP""'
	di `lossadj_name'


	//Column 2
	//No selection Correction, Restricted sample IV
	ivreghdfe acceptinsurance `fee' (lev_CIP_FE`adj_loss'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if `cross_state_var'>1 & capacity == 0, absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate2		

	//Column 1
	// No selection Correction, Restricted sample OLS
	areg acceptinsurance `fee' lev_CIP_FE`adj_loss' `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate1

			
	//Column 3
	// Selection Correction, Restricted sample OLS
	areg acceptinsurance `fee' lev_CIP_FE_nofe`adj_loss' `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate3


	//Column 4
	// Selection Correction, Restricted sample IV
	ivreghdfe acceptinsurance `fee' (lev_CIP_FE_nofe`adj_loss'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate4	


	// set output dir to avoid conflict on BEA server 
	cd `"$output/online_appendix"'
	#delimit ;

		//cross state table
		esttab mainxstate1 mainxstate2 mainxstate3 mainxstate4 using "online_appendix_`group'_xstate`adj_loss'.tex" ,  replace substitute(_ \_)
		numbers  nomtitles booktabs mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`lossadj_name' `fee_name')
		keep(Fee CIP ) 
		order(CIP Fee)
		coeflabels(Fee "$\log \pi$ index"
			   CIP "$\tau$ index"
	)
		se b(4) se(4) stats(instrument Sample pys_number N group_FE controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Group FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;

					
	#delimit cr

	}

	
	
	***********************************************************************************
	**** Cross-State Specification with CIP index in dollar and levels (Table S.19) ***
	***********************************************************************************
					
	//Column 2
	 //No selection Correction, No MCO, Restricted sample IV, No Size
	ivreghdfe acceptinsurance `fee' (`loss_dollar'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if `cross_state_var'>1 & capacity == 0, absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate2		

	//Column 1
	// No selection Correction, No MCO, Restricted sample OLS, No Size
	areg acceptinsurance `fee' `loss_dollar' `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate1

			
	//Column 3
	// Selection Correction, No MCO, Restricted sample OLS, No Size
	areg acceptinsurance `fee' `loss_dollar_heck' `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate3


	//Column 4
	// Selection Correction, No MCO, Restricted sample IV, No Size
	ivreghdfe acceptinsurance `fee' (`loss_dollar_heck'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate4	


	// set output dir to avoid conflict on BEA server 
	cd `"$output/online_appendix"'
	#delimit ;

		//cross state table
		esttab mainxstate1 mainxstate2 mainxstate3 mainxstate4 using "online_appendix_`group'_xstate_CIP_dollar.tex" ,  replace substitute(_ \_)
		numbers  nomtitles booktabs mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_level_name' `fee_name')
		keep(Fee CIP ) 
		order(CIP Fee)
		coeflabels(Fee "$\log \pi$ index"
			   CIP "CIP index"
	)
		se b(4) se(4) stats(instrument Sample pys_number N group_FE controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Group FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;

					
	#delimit cr


	
	
	***********************************************************************************************************
	**** Cross-State Specification with CIP index in dollar and levels and Fee index in levels (Table S.21) ***
	***********************************************************************************************************
					
	//Column 2
	//No selection Correction, Restricted sample IV
	ivreghdfe acceptinsurance `fee_level' (`loss_dollar'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if `cross_state_var'>1 & capacity == 0, absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate2		

	//Column 1
	// No selection Correction, Restricted sample OLS
	areg acceptinsurance `fee_level' `loss_dollar' `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate1

			
	//Column 3
	// Selection Correction, Restricted sample OLS
	areg acceptinsurance `fee_level' `loss_dollar_heck' `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate3


	//Column 4
	// Selection Correction, Restricted sample IV
	ivreghdfe acceptinsurance `fee_level' (`loss_dollar_heck'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate4	


	// set output dir to avoid conflict on BEA server 
	cd `"$output/online_appendix"'
	#delimit ;

		//cross state table
		esttab mainxstate1 mainxstate2 mainxstate3 mainxstate4 using "online_appendix_`group'_xstate_fee_level_CIP_dollar.tex" ,  replace substitute(_ \_)
		numbers  nomtitles booktabs mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_level_name' `fee_level_name')
		keep(Fee CIP ) 
		order(CIP Fee)
		coeflabels(Fee "$\pi$ index"
			   CIP "CIP index"
	)
		se b(4) se(4) stats(instrument Sample pys_number N group_FE controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Group FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;

					
	#delimit cr


}


****************************************************************************
**** Cross-State Specification with group-year fixed effects (Table S.14)***
****************************************************************************
foreach group of var group3y { 
	
	if "`group'" == "group3y"  {
		local cross_state_var totstates3
		local definition "MDPPAS_Tax_ID"
		local group_table "Tax ID"
	}

	//Cross State Regressions

	local loss_denial v_initial_den_CIP
	local loss lev_CIP_FE
	local loss_heck lev_CIP_FE_nofe
	local fee pi_c_FE_cpt_line
	
	local loss_name `loss' CIP `loss_heck' CIP 
	local fee_name `fee' Fee

	
	//Column 2
	 //No selection Correction, No MCO, Restricted sample IV, No Size
	ivreghdfe acceptinsurance `fee' (`loss'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' if `cross_state_var'>1 & capacity == 0, absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate2		

	//Column 1
	// No selection Correction, No MCO, Restricted sample OLS, No Size
	areg acceptinsurance `fee' `loss' `health_ins_market_year' `health_prov_year' `ses_year' if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate1
			
	//Column 3
	// Selection Correction, No MCO, Restricted sample OLS, No Size
	areg acceptinsurance `fee' `loss_heck' `health_ins_market_year' `health_prov_year' `ses_year' if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate3

	//Column 4
	// Selection Correction, No MCO, Restricted sample IV, No Size
	ivreghdfe acceptinsurance `fee' (`loss_heck'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' if e(sample), absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate4	
	

	// set output dir to avoid conflict on BEA server 
	cd `"$output/online_appendix"'
	#delimit ;

		//cross state table
		esttab mainxstate1 mainxstate2 mainxstate3 mainxstate4 using `"online_appendix_`group'_year-group-fe.tex"' ,  replace substitute(_ \_)
		numbers  nomtitles booktabs mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_name' `fee_name')
		keep(Fee CIP ) 
		order(CIP Fee)
		coeflabels(Fee "$\log \pi$ index"
			   CIP "$\tau$ index"
	)
		se b(4) se(4 ) stats(instrument Sample pys_number N group_FE controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Group FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
						
					
	#delimit cr

	
	
}



****************************************************************
**** Cross-State Specifications MCO-FFS Index (Table S.23) *****
****************************************************************
clear all 
//use the mco enrolle/medicaid enrollee data to create mco shares 
use `"$parent/data/mco_data/medicaid_enrollment.dta"', clear 
merge 1:1 year payee_state_cd using `"$parent/data/mco_data/medicaid_mco_enrollment.dta"', nogen 

gen mco_share = mco_enroll/medicaid_enroll
replace mco_share = 1 if mco_share > 1 

keep payee_state_cd year  mco_share

tempfile mco_share_file
save `mco_share_file'


use `"$x_state_data/$x_state_file_regression"', clear 


gen accept_medicare = 1 if medicare == "Y"
replace accept_medicare = 0 if medicare == "N"
replace accept_medicare = . if acceptinsurance == . 
bys npimedicare_id: gegen mean_medicare = mean(accept_medicare)
gen capacity = 1 - mean_medicare


*merge in the indices 
merge m:1 payee_state_cd using `"$indices/fee_indices_medicaid.dta"', keepusing(pi*)
keep if _merge == 3
drop _merge 

merge m:1 payee_state_cd using `"$mcoindices/CIP_indices_medicaid.dta"', keepusing(*CIP*)
keep if _merge == 3
drop _merge 


//merge controls 
merge m:1 year FIPS using `"$control_var/county_controls.dta"'
keep if _merge == 3 // all data from master file merge 
drop _merge 

destring statefips, replace  
merge m:1 year statefips using `"$control_var/control_vars_state.dta"'
keep if _merge == 3 //all data from the master file merge 
drop _merge 

merge m:1 year payee_state_cd using `mco_share_file'
keep if _merge == 3 //all data from the master file merge 
drop _merge 


*Use only TIN info to connect groups
gen skaGroup3a="MDPPASTIN"+tin  if ~missing(tin)

cap drop group?
gegen group3 = group(skaGroup3a)


cap drop totstates?
sort skaGroup3 paystateid
by skaGroup3 paystateid: gen markstate=_n==1
by skaGroup3: egen totstates3=sum(markstate)
drop markstate


gen ltotdocsALL1=lnumdocs

gen kaiser=CODE3=="KAI"
drop if kaiser


****locals for Oster test
local years "2009.year 2010.year 2011.year 2012.year 2013.year 2014.year 2015.year"

cap drop MDsPerCapita
gen MDsPerCapita = tot_active_md/county_pop

foreach c of varlist veteran unempl poverty white {
	
	replace `c' = `c'/county_pop
}


unab controllist: mean_ma_pen mean_number_mediciad_elig mean_dual mean_unins65 mean_number_medicare_elig mean_medicare_enrol mco_share
unab year_controllist: hinsva hinscare hinscaid hcovany veteran unempl med_hh_ince avg_hhc county_pop tot_active_md poverty white


**enusre controls are notm issingsortsort npimedicare_id YearPost
foreach var of varlist `controllist' `year_controllist' {
	
	keep if `var' != . 
}

unab health_ins_market_year: hinscaid hcovany hinscare avg_hhc mean_ma_pen mean_number_mediciad_elig mean_dual mean_unins65 mean_number_medicare_elig mean_medicare_enrol 
unab health_prov_year: tot_active_md MDsPerCapita
unab ses_year: unempl white county_pop veteran poverty med_hh_ince
	

foreach group of var group3 { 
	

	
	
	else if "`group'" == "group3" {
		local cross_state_var totstates3
		local definition "MDPPAS_Tax_ID"
		local group_table "Tax ID"

	}
	
	
	xtset `group'

	cap drop physind 
	cap drop phys_count
	cap drop group_ind
	gen group_ind = 1 if `cross_state_var'>1 & `group' != . & capacity == 0 
	gen physind = 0
	bys npimedicare_id group_ind: replace physind = 1 if _n == 1 
	replace physind = 0 if group_ind != 1
	gegen phys_count = sum(physind)
	local physcount : di %7.0fc phys_count
	di `physcount'
		
		
	*************************************************************
	***** Appendix Table MCO/FFS Specific Index and Control *****
	*************************************************************
					
	local loss_denial v_initial_den_CIP
	local loss lev_CIP_FE
	local loss_heck lev_CIP_FE_nofe
	local fee pi_c_FE_cpt_line

	local loss_name `loss' CIP `loss_heck' CIP 
	local fee_name `fee' Fee




	//Column 2
	//No selection Correction, Restricted sample IV
	ivreghdfe acceptinsurance `fee' (`loss'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' mco_share i.year if `cross_state_var'>1 & capacity == 0, absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	estadd local mco_share "Yes"
	eststo mainxstate2		

	//Column 1
	// No selection Correction, Restricted sample OLS
	areg acceptinsurance `fee' `loss' `health_ins_market_year' `health_prov_year' `ses_year' mco_share i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	estadd local mco_share "Yes"
	eststo mainxstate1

			
	//Column 3
	// Selection Correction, Restricted sample OLS
	areg acceptinsurance `fee' `loss_heck' `health_ins_market_year' `health_prov_year' `ses_year' mco_share i.year if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	estadd local mco_share "Yes"
	eststo mainxstate3


	//Column 4
	// Selection Correction, Restricted sample IV
	ivreghdfe acceptinsurance `fee' (`loss_heck'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' mco_share i.year if e(sample), absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	estadd local mco_share "Yes"
	eststo mainxstate4	


	// set output dir to avoid conflict on BEA server 
	cd `"$output/online_appendix"'
	#delimit ;

		//cross state table
		esttab mainxstate1 mainxstate2 mainxstate3 mainxstate4 using `"online_appendix_`group'_xstate_mcoffsindex.tex"' , replace substitute(_ \_)
		numbers  nomtitles booktabs mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_name' `fee_name')
		keep(Fee CIP ) 
		order(CIP Fee)
		coeflabels(Fee "$\log \pi$ index"
			   CIP "$\tau$ index"
	)

		se b(4) se(4 ) stats(instrument Sample pys_number N group_FE controls mco_share phys_fe_index sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Group FE}" "\footnotesize{Controls}" "\footnotesize{Control for State Level MCO Share}" "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
					
	#delimit cr
		

}








*******************************************************************
*******************************************************************
********* SPECIALTY SPECIFIC FEE INDEX ****************************
********* SPECIALTY SPECIFIC PHYSICIAN SHARE (Table S.15) *********
*******************************************************************
*******************************************************************


clear all

// prepare the index file with the specialty specific indices by kkeeping only medicaid 
use `"$indices/fee_indices_specialty.dta"'
keep if pay_type_pooled == 2 
tempfile specfee
save `specfee'

use `"$x_state_data/$x_state_file_regression"', clear 
gen accept_medicare = 1 if medicare == "Y"
replace accept_medicare = 0 if medicare == "N"
replace accept_medicare = . if acceptinsurance == . 
bys npimedicare_id: gegen mean_medicare = mean(accept_medicare)
gen capacity = 1 - mean_medicare


//determine share of PCP physicians in state specific locations
gen pcp = 0
replace pcp = 1 if final_specialty == "Family Practice" | final_specialty == "Internal Medicine" | final_specialty == "Pediatric Medicine"


*merge in the indices 
merge m:1 payee_state_cd pcp using `specfee', keepusing(pi*)
keep if _merge == 3
drop _merge 

merge m:1 payee_state_cd using `"$indices/CIP_indices_medicaid.dta"', keepusing(*CIP*)
keep if _merge == 3
drop _merge 


//merge controls 
merge m:1 year FIPS using `"$control_var/county_controls.dta"'
keep if _merge == 3 // all data from master file merge 
drop _merge 

destring statefips, replace  
merge m:1 year statefips using `"$control_var/control_vars_state.dta"'
keep if _merge == 3 //all data from the master file merge 
drop _merge 



//Use only TIN info to connect groups
gen skaGroup3a=string(ID)
replace skaGroup3a="MDPPASTIN"+tin  if ~missing(tin)
 
//drop missing
drop if missing(paystateid)

cap drop group?
gegen group3 = group(skaGroup3a)
gegen group3y = group(skaGroup3a year)


cap drop totstates?
//cross state variable 
cap drop totstates?
	
sort year group3 paystateid
by year group3 paystateid: gen markstate=_n==1
by year group3: egen totstates3=sum(markstate)
drop markstate


gen ltotdocsALL1=lnumdocs

gen kaiser=CODE3=="KAI"
drop if kaiser


****locals for Oster test
local years "2009.year 2010.year 2011.year 2012.year 2013.year 2014.year 2015.year"

cap drop MDsPerCapita
gen MDsPerCapita = tot_active_md/county_pop

foreach c of varlist veteran unempl poverty white {
	
	replace `c' = `c'/county_pop
}


unab controllist: mean_ma_pen mean_number_mediciad_elig mean_dual mean_unins65 mean_number_medicare_elig mean_medicare_enrol 
unab year_controllist: hinsva hinscare hinscaid hcovany veteran unempl med_hh_ince avg_hhc county_pop tot_active_md poverty white


**enusre controls are notm issingsortsort npimedicare_id YearPost
foreach var of varlist `controllist' `year_controllist' {
	
	keep if `var' != . 
}

unab health_ins_market_year: hinscaid hcovany hinscare avg_hhc mean_ma_pen mean_number_mediciad_elig mean_dual mean_unins65 mean_number_medicare_elig mean_medicare_enrol 
unab health_prov_year: tot_active_md MDsPerCapita
unab ses_year: unempl white county_pop veteran poverty med_hh_ince

local loss_denial v_initial_den_CIP
local loss lev_CIP_FE
local loss_heck lev_CIP_FE_nofe
local fee pi_c_FE_cpt_line

local loss_name `loss' CIP `loss_heck' CIP 
local fee_name `fee' Fee
	
foreach group of var group3 { 
	
	else if "`group'" == "group3" {
		local cross_state_var totstates3
		local definition "MDPPAS_Tax_ID"
		local group_table "Tax ID"

	}
	

	xtset `group'


	cap drop physind 
	cap drop phys_count
	cap drop group_ind
	gen group_ind = 1 if `cross_state_var'>1 & `group' != . & capacity == 0 
	gen physind = 0
	bys npimedicare_id group_ind: replace physind = 1 if _n == 1 
	replace physind = 0 if group_ind != 1
	gegen phys_count = sum(physind)
	local physcount : di %7.0fc phys_count
	di `physcount'

	bys payee_state_cd `group': gegen pcp_`group' = mean(pcp)


	*******************************************************
	**** Appendix Table S.6 -- PCP specific Fee Index *****
	*******************************************************


	foreach adj_fee of varlist pi_c_FE_cpt_line_pcp {
		
		local adj_fee_name `"`adj_fee' Fee"'
		if "`adj_fee'" == "pi_c_FE_cpt_line_pcp" {
			local x "pcp_fee"
		}

		
			
		//Column 2
		 //No selection Correction, No MCO, Restricted sample IV, No Size
		ivreghdfe acceptinsurance `adj_fee' (`loss'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if `cross_state_var'>1 & capacity == 0, absorb(`group') cluster(paystateid) first
		estadd local pys_number "`physcount'"
		estadd local sel_cor_index "No"
		estadd local phys_fe_index "Yes"
		estadd local instrument "2SLS"
		estadd local group_FE "Yes"
		estadd local controls "Yes"
		estadd local Sample "Yes"
		eststo mainxstate2		

		//Column 1
		// No selection Correction, No MCO, Restricted sample OLS, No Size
		areg acceptinsurance `adj_fee' `loss' `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') vce(cluster paystateid)
		estadd local pys_number "`physcount'"
		estadd local sel_cor_index "No"
		estadd local phys_fe_index "Yes"
		estadd local instrument "OLS"
		estadd local group_FE "Yes"
		estadd local controls "Yes"
		estadd local Sample "Yes"
		eststo mainxstate1

				
		//Column 3
		// Selection Correction, No MCO, Restricted sample OLS, No Size
		areg acceptinsurance `adj_fee' `loss_heck' `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') vce(cluster paystateid)
		estadd local pys_number "`physcount'"
		estadd local sel_cor_index "Yes"
		estadd local phys_fe_index "No"
		estadd local instrument "OLS"
		estadd local group_FE "Yes"
		estadd local controls "Yes"
		estadd local Sample "Yes"
		eststo mainxstate3


		//Column 4
		// Selection Correction, No MCO, Restricted sample IV, No Size
		ivreghdfe acceptinsurance `adj_fee' (`loss_heck'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(`group') cluster(paystateid) first
		estadd local pys_number "`physcount'"
		estadd local sel_cor_index "Yes"
		estadd local phys_fe_index "No"
		estadd local instrument "2SLS"
		estadd local group_FE "Yes"
		estadd local controls "Yes"
		estadd local Sample "Yes"
		eststo mainxstate4	


		// set output dir to avoid conflict on BEA server 
		cd `"$output/online_appendix"'
		#delimit ;

			//cross state table
			esttab mainxstate1 mainxstate2 mainxstate3 mainxstate4 using "online_appendix_`group'_xstate_`x'.tex" ,  replace substitute(_ \_)
			numbers  nomtitles booktabs mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
			rename(`loss_name' `adj_fee_name')
			keep(Fee CIP ) 
			order(CIP Fee)
			coeflabels(Fee "$\log \pi$ index"
				   CIP "$\tau$ index"
		)
			se b(4) se(4 ) stats(instrument Sample pys_number N group_FE controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Group FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
		starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;

						
		#delimit cr
		
	}



	//Column 2
	 //No selection Correction, No MCO, Restricted sample IV, No Size
	ivreghdfe acceptinsurance `fee' (`loss'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year pcp_`group' if `cross_state_var'>1 & capacity == 0, absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate2		

	//Column 1
	// No selection Correction, No MCO, Restricted sample OLS, No Size
	areg acceptinsurance `fee' `loss' `health_ins_market_year' `health_prov_year' `ses_year' i.year pcp_`group' if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "No"
	estadd local phys_fe_index "Yes"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate1

			
	//Column 3
	// Selection Correction, No MCO, Restricted sample OLS, No Size
	areg acceptinsurance `fee' `loss_heck' `health_ins_market_year' `health_prov_year' `ses_year' i.year  pcp_`group' if e(sample), absorb(`group') vce(cluster paystateid)
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "OLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate3


	//Column 4
	// Selection Correction, No MCO, Restricted sample IV, No Size
	ivreghdfe acceptinsurance `fee' (`loss_heck'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year pcp_`group' if e(sample), absorb(`group') cluster(paystateid) first
	estadd local pys_number "`physcount'"
	estadd local sel_cor_index "Yes"
	estadd local phys_fe_index "No"
	estadd local instrument "2SLS"
	estadd local group_FE "Yes"
	estadd local controls "Yes"
	estadd local Sample "Yes"
	eststo mainxstate4	


	// set output dir to avoid conflict on BEA server 
	cd `"$output/online_appendix"'
	#delimit ;

		//cross state table
		esttab mainxstate1 mainxstate2 mainxstate3 mainxstate4 using "online_appendix_`group'_xstate_pcp_share.tex" ,  replace substitute(_ \_)
		numbers  nomtitles booktabs mgroups("Accept Medicaid Patients?", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
		rename(`loss_name' `fee_name')
		keep(Fee CIP ) 
		order(CIP Fee)
		coeflabels(Fee "$\log \pi$ index"
			   CIP "$\tau$ index"
	)
		se b(4) se(4 ) stats(instrument Sample pys_number N group_FE controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{Estimator}" "\footnotesize{Subsample Accepting Medicare}" "\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "\footnotesize{Group FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;

					
	#delimit cr
		
}







*******************************************************
*******************************************************
*******************************************************
*** Intensive Margin -- Share of Medicaid Patients ****
******** Online Appendix Tables S.24 and S.25 *********
*******************************************************
*******************************************************

use `"$output/data/$line_file"', clear

//visit level data 
bys visit_billing_id: keep if _n == 1 

//set main state for provider 
encode payee_state_cd, gen(state_numeric)
bys idw_provider_id year: gegen main_state = mode(state_numeric), min
drop if main_state != state_numeric

bys idw_provider_id year: egen sds= sd(state_numeric)
assert sds == 0 | sds == .

// create medicaid dummy
gen medicaid = 0
replace medicaid = 1 if pay_type_pooled == 2 

bys idw_provider_id year: gegen share_medicaid = mean(medicaid)
 
gen FIPS=real(COUNTY)
sort year FIPS

*merge in the indices 
merge m:1 payee_state_cd using `"$indices/fee_indices_medicaid.dta"', keepusing(pi*)
keep if _merge == 3
drop _merge 

merge m:1 payee_state_cd using `"$indices/CIP_indices_medicaid.dta"', keepusing(*CIP*)
keep if _merge == 3
drop _merge 


//merge controls 
merge m:1 year FIPS using `"$control_var/county_controls.dta"'
keep if _merge == 3 // all data from master file merge 
drop _merge 

destring statefips, replace  
merge m:1 year statefips using `"$control_var/control_vars_state.dta"'
keep if _merge == 3 //all data from the master file merge 
drop _merge 


//keep one pysician observation per year 
bys idw_provider_id year: gen number_visits = _N
bys idw_provider_id year: keep if _n == 1 

	
//generate control variables as shares 
cap drop MDsPerCapita
gen MDsPerCapita = tot_active_md/county_pop

foreach c of varlist veteran unempl poverty white {
	
	replace `c' = `c'/county_pop
}

foreach var of varlist `year_controllist' MDsPerCapita v_initial_den_CIP lev_CIP_FE lev_CIP_FE_nofe pi_c_FE_cpt_line {
	
	keep if `var' != . 
	
}

//create variable for number of states 
cap drop totstates?	
sort year skaGroup3a payee_state_cd
by year skaGroup3a payee_state_cd: gen markstate=_n==1
by year skaGroup3a: egen totstates3=sum(markstate)
drop markstate

//numeric group variable 
cap drop group?
gegen group3 = group(skaGroup3a)


//number of physicians
cap drop physind 
cap drop phys_count
cap drop group_ind
gen group_ind = 1 if totstates3>1 & group3 != .
gen physind = 0
bys idw_provider_id group_ind: replace physind = 1 if _n == 1 
replace physind = 0 if group_ind != 1
gegen phys_count = sum(physind)
local physcount : di %7.0fc phys_count
di `physcount'

cap drop physind 
cap drop phys_count
cap drop group_ind
gen group_ind = 1 if totstates3>1 & group3 & share_medicaid >0
gen physind = 0
bys idw_provider_id group_ind: replace physind = 1 if _n == 1 
replace physind = 0 if group_ind != 1
gegen phys_count = sum(physind)
local physcount_rest : di %7.0fc phys_count
di `physcount_rest'


// set controls 
unab health_ins_market_year: hinscaid hcovany hinscare avg_hhc 
unab health_prov_year: tot_active_md MDsPerCapita
unab ses_year: unempl white county_pop veteran poverty med_hh_ince

//set locals 
local loss_denial v_initial_den_CIP
local loss lev_CIP_FE
local loss_heck lev_CIP_FE_nofe
local fee pi_c_FE_cpt_line

local loss_name `loss' CIP `loss_heck' CIP 
local fee_name `fee' Fee



// physician level regressions
ivreghdfe share_medicaid `fee' (`loss'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if totstates3 > 1, absorb(group3) cluster(payee_state_cd) first
estadd local pys_number "`physcount'"
estadd local sel_cor_index "No"
estadd local phys_fe_index "Yes"
estadd local instrument "2SLS"
estadd local group_FE "Yes"
estadd local controls "Yes"
eststo col2

areg share_medicaid `loss' `fee' i.year `health_prov_year' `health_ins_market_year' `ses_year' if e(sample), cluster(payee_state_cd) absorb(group3)
estadd local pys_number "`physcount'"
estadd local sel_cor_index "No"
estadd local phys_fe_index "Yes"
estadd local instrument "OLS"
estadd local group_FE "Yes"
estadd local controls "Yes"
eststo col1

areg share_medicaid `loss_heck' `fee' i.year `health_prov_year' `health_ins_market_year' `ses_year' if e(sample), cluster(payee_state_cd) absorb(group3)
estadd local pys_number "`physcount'"
estadd local sel_cor_index "Yes"
estadd local phys_fe_index "No"
estadd local instrument "OLS"
estadd local group_FE "Yes"
estadd local controls "Yes"
eststo col3

ivreghdfe share_medicaid `fee' (`loss_heck'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(group3) cluster(payee_state_cd) first
estadd local pys_number "`physcount'"
estadd local sel_cor_index "Yes"
estadd local phys_fe_index "No"
estadd local instrument "2SLS"
estadd local group_FE "Yes"
estadd local controls "Yes"
eststo col4



//set cd and export table 
cd `"$output/online_appendix"'
#delimit;
	esttab col1 col2 col3 col4 using `"share_medicaid.tex"' ,  replace substitute(_ \_)
	nonumbers nomtitles
	mgroups("Share of Medicaid Patients" , pattern(1 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	rename(`loss_name')
	keep(CIP pi_c_FE_cpt_line) 
	order(CIP pi_c_FE_cpt_line)
	coeflabels(CIP "$\tau$ index" 
			   pi_c_FE_cpt_line "$\log \pi$ index"
	)
	se b(5) se(5) stats(pys_number N instrument group_FE controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "Estimator" "\footnotesize{Group FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
#delimit cr 

eststo clear 




// physician level regressions
ivreghdfe share_medicaid `fee' (`loss'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if totstates3 > 1 & share_medicaid> 0, absorb(group3) cluster(payee_state_cd) first
estadd local pys_number "`physcount_rest'"
estadd local sel_cor_index "No"
estadd local phys_fe_index "Yes"
estadd local instrument "2SLS"
estadd local group_FE "Yes"
estadd local controls "Yes"
eststo col2


areg share_medicaid `loss' `fee' i.year `health_prov_year' `health_ins_market_year' `ses_year' if e(sample), cluster(payee_state_cd) absorb(group3)
estadd local pys_number "`physcount_rest'"
estadd local sel_cor_index "No"
estadd local phys_fe_index "Yes"
estadd local instrument "OLS"
estadd local group_FE "Yes"
estadd local controls "Yes"
eststo col1

areg share_medicaid `loss_heck' `fee' i.year `health_prov_year' `health_ins_market_year' `ses_year' if e(sample), cluster(payee_state_cd) absorb(group3)
estadd local pys_number "`physcount_rest'"
estadd local sel_cor_index "Yes"
estadd local phys_fe_index "No"
estadd local instrument "OLS"
estadd local group_FE "Yes"
estadd local controls "Yes"
eststo col3

ivreghdfe share_medicaid `fee' (`loss_heck'=`loss_denial') `health_ins_market_year' `health_prov_year' `ses_year' i.year if e(sample), absorb(group3) cluster(payee_state_cd) first
estadd local pys_number "`physcount_rest'"
estadd local sel_cor_index "Yes"
estadd local phys_fe_index "No"
estadd local instrument "2SLS"
estadd local group_FE "Yes"
estadd local controls "Yes"
eststo col4


//set cd and export table 
cd `"$output/online_appendix"'
#delimit;
	esttab col1 col2 col3 col4 using `"share_medicaid_conditional.tex"' ,  replace substitute(_ \_)
	nonumbers nomtitles
	mgroups("Share of Medicaid Patients" , pattern(1 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	rename(`loss_name')
	keep(CIP pi_c_FE_cpt_line) 
	order(CIP pi_c_FE_cpt_line)
	coeflabels(CIP "$\tau$ index" 
			   pi_c_FE_cpt_line "$\log \pi$ index"
	)
	se b(5) se(5) stats(pys_number N instrument group_FE controls phys_fe_index  sel_cor_index , fmt(%7.0fc %9.0fc) labels("\footnotesize{N.~Physicians}" "\footnotesize{N.~Physicians-Years}" "Estimator" "\footnotesize{Group FE}" "\footnotesize{Controls}"  "\midrule  $\tau$ index: \\ \multicolumn{1}{r}{\footnotesize{Physician FE}}" "\multicolumn{1}{r}{\footnotesize{Selection Correction}}" ))
	starlevels(* 0.10 ** 0.05 *** 0.01)  label varwidth(20) nonotes;
#delimit cr 





cap log close 
